//
//  YSSummaryViewController.m
//  YSBookRead
//
//  Created by MOLBASE on 2018/11/15.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "YSSummaryViewController.h"
#import "YSSummaryCell.h"
#import "YSReadOperation.h"
#import "YSHUDManager.h"

@interface YSSummaryViewController () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *summaryArray;
@end

@implementation YSSummaryViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YSSummaryCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([YSSummaryCell class])];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.rowHeight = 60;

    [self getBookSummary];
}

- (void)getBookSummary {
    [self.readOperation getBookSummaryList:^(NSArray *summarys, BOOL isSuccess) {
        if (!isSuccess) {
            [YSHUDManager showTipHUD:@"请求章节出错" afterDelay:1];
        }

        self.summaryArray = summarys;
        [self.tableView reloadData];
    }];
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.summaryArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YSSummaryCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YSSummaryCell class]) forIndexPath:indexPath];
    cell.summaryModel = self.summaryArray[indexPath.row];
    cell.isSelectSummary = [cell.summaryModel.ID isEqualToString:_summaryM.ID];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

     YSBookSummaryModel *summaryModel = self.summaryArray[indexPath.row];

    [self dismissViewControllerAnimated:YES completion:^{
        if (summaryModel && self.updateSelectSummary) {
            self.updateSelectSummary(summaryModel);
        }
    }];
}

- (IBAction)backButtonDidClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
